package com.star.core.base;


import com.star.common.enums.BizStatus;
import com.star.common.exception.BizException;
import com.star.security.context.TokenContextHolder;
import com.star.security.user.Users;

/**
 * 基本控制器
 *
 * @author yangzj
 */
public class BaseController {

    /**
     * 获取用户
     *
     * @return {@link Users}
     */
    protected final Users getUser() {
        Object principal = TokenContextHolder.getPrincipal();
        if (principal == null) {
            throw new BizException(BizStatus.NOT_LOGIN);
        }
        return (Users) principal;
    }

    /**
     * 用户id
     *
     * @return {@link Long}
     */
    protected final Long getUserId() {
        return getUser().getId();
    }

    /**
     * 超管为空
     *
     * @return {@link Long}
     */
    protected final Long getUserIdIfAdminNull() {
        return isSuperAdmin() ? null : getUserId();
    }

    /**
     * 是否管理员
     *
     * @return boolean
     */
    protected final boolean isSuperAdmin() {
        return getUser().isSuperAdmin();
    }
}
